/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dto.StudentDTO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

/**
 *
 * @author XMANI
 */
public class StudentDao {

    Connection cn = null;
    String fileNormal = "normal.txt";
    String fileEncrypt = "encrypt.txt";
    String fileDecrypt = "decrypt.txt";

    public StudentDao() {
        try {
//            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//            cn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=DBSJ_Thi1", "sa", "123");

            Class.forName("org.apache.derby.jdbc.ClientDriver");
            cn= DriverManager.getConnection("jdbc:derby://localhost:1527/CSDL_DBSL", "app", "app");

            if (cn != null) {
                System.out.println("ket noi");
            }
        } catch (SQLException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ArrayList<StudentDTO> getStudent() {
        ArrayList<StudentDTO> list = new ArrayList<StudentDTO>();
        try {
            Statement st = cn.createStatement();
            ResultSet rs = st.executeQuery("Select * from StudentDetails");
            while (rs.next()) {
                StudentDTO stu = new StudentDTO();
                stu.setName(rs.getString(1));
                stu.setAddress(rs.getString(2));
                stu.setBirthday(rs.getDate(3));
                list.add(stu);
            }


        } catch (SQLException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;



    }

    public void saveFile() {
        FileWriter f = null;
        try {
            ArrayList<StudentDTO> list = getStudent();
            f = new FileWriter(fileNormal);
            for (StudentDTO s : list) {
                String a = s.getName() + "|" + s.getAddress() + "|" + s.getBirthday().toString() + "\n";
                f.write(a);
            }
        } catch (IOException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                f.close();
            } catch (IOException ex) {
                Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void encrypt() {

        saveFile();

        FileInputStream in = null;
        FileOutputStream ou = null;


        try {



            PBEKeySpec pbeKeySpec;
            PBEParameterSpec pbeParameterSpec;
            SecretKeyFactory keyFac;


            char[] password = "admin".toCharArray();
            byte[] salt = {
                (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
                (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99
            };


            int count = 20;
            pbeKeySpec = new PBEKeySpec(password);
            keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
            SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
            pbeParameterSpec = new PBEParameterSpec(salt, count);
            Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
            pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParameterSpec);




            in = new FileInputStream(fileNormal);
            byte[] plainText = new byte[in.available()];
            in.read(plainText);

            byte[] encryptText = pbeCipher.doFinal(plainText);


            ou = new FileOutputStream(fileEncrypt);
            ou.write(encryptText);
        } catch (IllegalBlockSizeException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadPaddingException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidAlgorithmParameterException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchPaddingException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeySpecException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                in.close();
                ou.close();
            } catch (IOException ex) {
                Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }
    public void decriypt(){



        FileInputStream in = null;
        FileOutputStream ou = null;
        try {
            PBEKeySpec pbeKeySpec;
            PBEParameterSpec pbeParameterSpec;
            SecretKeyFactory keyFac;
            char[] password = "admin".toCharArray();
            byte[] salt = {
                (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
                (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99
            };
            int count = 20;
            pbeKeySpec = new PBEKeySpec(password);
            keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
            SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
            pbeParameterSpec = new PBEParameterSpec(salt, count);
            Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
            pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParameterSpec);
            in = new FileInputStream(fileEncrypt);
            byte[] plainText = new byte[in.available()];
            in.read(plainText);
            byte[] encryptText = pbeCipher.doFinal(plainText);
            ou = new FileOutputStream(fileDecrypt);
            ou.write(encryptText);
        } catch (IllegalBlockSizeException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadPaddingException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidAlgorithmParameterException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchPaddingException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeySpecException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                in.close();
                ou.close();
            } catch (IOException ex) {
                Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


    }
}
